app.service('goodsDetailAddService',['$http','$q','utilService',function($http,$q,utilService){
    var _this_=this;
    _this_.goods={
        isOpenSku:0,
        isFreeShipping:0,
        isOpenMember:0,
        isRecommend:0,
        isHelpPeasant:0,
        isOpenRebate:0,
        isHelpPeasant:0,
        postageType:0,
        isOpenSell:0,
        profit:0,
        systemTypeId:[],
        postageWeight:0.000,
        tagIds:[]
    };

    //查询店铺信息
    _this_.queryShop= function ($scope) {
        var deferred = $q.defer();

        utilService.get({
            url:$scope.app.domain + 'chain/session'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })

        return deferred.promise;
    }



    //查询品牌信息
    _this_.querybrands= function ($scope) {
        var deferred = $q.defer();

        utilService.get({
            url:$scope.app.domain + 'chain/brand/alls'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })

        return deferred.promise;
    }

    //查询商品分类
    _this_.queryType= function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'chain/type/goods/type'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }

    //查询系统分类
    _this_.querySystemType= function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/type/name'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }

    //查询运费模板
    _this_.queryPostageTemplate= function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'postage/template/all'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }

    //添加商品
    _this_.addGoods=function($scope){

        if(_this_.goods.postageType==0){
            delete _this_.goods.postagePrice;
            delete _this_.goods.postageWeight;
            delete _this_.goods.postageTemplateId;
        }else if(_this_.goods.postageType==1){
            delete _this_.goods.postageWeight;
            delete _this_.goods.postageTemplateId;
        }else if(_this_.goods.postageType==2){
            delete _this_.goods.postagePrice;
        }

        //若为普通商品清除让利值
        if(_this_.goods.isOpenRebate == 0 && _this_.goods.isOpenSell == 0){
            delete _this_.goods.profit;
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"chain/goods",
            data:_this_.goods
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


    //添加规格
    _this_.saveGoodsSku=function($scope,goodsId) {

        var goodsSkuItemsArr=new Array();
        for (var i = 0; i < $scope.skus.length; i++) {
            for(var j=0;j<$scope.skus[i].skuItems.length;j++){
                var goodsSkuItems=new Object();
                goodsSkuItems.sort=$scope.skus[i].skuItems[j].sort;
                goodsSkuItems.skuItemsSort=$scope.skus[i].skuItems[j].skuItemsSort;
                goodsSkuItems.chainSkuId=$scope.skus[i].skuItems[j].chainSkuId;
                goodsSkuItems.chainSkuItemsId=$scope.skus[i].skuItems[j].id;
                goodsSkuItems.chainGoodsId=$scope.chainGoodsId;
                goodsSkuItemsArr.push(goodsSkuItems);
            }
        }


        var skuContentsAll=new Array();
        for(var i=0;i<$scope.skuContents.length;i++){
            var obj=new Object();
            angular.copy($scope.skuContents[i],obj);
            obj.chainGoodsId=$scope.chainGoodsId;
            delete obj.goodsSkuItems;
            delete obj.$$hashKey;
            skuContentsAll.push(obj);
        }

        var goodsSkuContentVO=new Object();
        goodsSkuContentVO.chainGoodsId=$scope.chainGoodsId;
        goodsSkuContentVO.chainGoodsSkuItemses=JSON.stringify(goodsSkuItemsArr);
        goodsSkuContentVO.chainSkuContents=JSON.stringify(skuContentsAll);

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"chain/goodssku",
            data:goodsSkuContentVO
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;

    }



}]);

